﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace XBoshi.SecurityCommon
{
    public class MD5Class
    {
        public static string GetMD5Hash(String oringString)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] data = System.Text.Encoding.Default.GetBytes(oringString);
            byte[] md5hash = md5.ComputeHash(data);
            return BitConverter.ToString(md5hash).Replace("-", "").ToLower();     
        }

       private static  Byte[] key1 = { 12, 23, 34, 45, 56, 67, 78, 89 };
       private static  Byte[] iv = { 120, 230, 10, 1, 10, 20, 30, 40 };
       private static Byte[] key2 = System.Text.Encoding.ASCII.GetBytes("X-Boshi1");
       private static Byte[] key3 = System.Text.Encoding.ASCII.GetBytes("X-Boshi2");
 


        /// <summary>
        /// 
        /// </summary>
        /// <param name="oringString"></param>
        /// <returns></returns>
        public static string GetDSAHash(String oringString)
        {
            byte[] strs = Encoding.Unicode.GetBytes(oringString);
            DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();

            ICryptoTransform transform = desc.CreateEncryptor(key1, iv);//加密对象
            CryptoStream cStream = new CryptoStream(mStream, transform, CryptoStreamMode.Write);
            cStream.Write(strs, 0, strs.Length);
            cStream.FlushFinalBlock();
            return Convert.ToBase64String(mStream.ToArray());
        }


        public void SetKeys(byte[] ivs)
        { 
        
        }

        public void SetIVS(byte[] ivs)
        {

        }

        public byte[] GetKeys(int keyTypes) {
            switch(keyTypes){
                case 0 :return key1;
                case 1 :return key2;
                case 2 :return key3;
            }
            return iv;
        } 



        
    /// <summary>
    /// 
    /// </summary>
    /// <param name="str"></param>
    /// <param name="keys"></param>
    /// <param name="ivs"></param>
    /// <returns></returns>
        public static string MyDESCrypto(string str, byte[] keys, byte[] ivs)
        {
            //加密
            byte[] strs = Encoding.Unicode.GetBytes(str);
            DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();

            ICryptoTransform transform = desc.CreateEncryptor(keys, iv);//加密对象
            CryptoStream cStream = new CryptoStream(mStream, transform, CryptoStreamMode.Write);
            cStream.Write(strs, 0, strs.Length);
            cStream.FlushFinalBlock();
            return Convert.ToBase64String(mStream.ToArray());
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <param name="keys"></param>
        /// <param name="ivs"></param>
        /// <returns></returns>
       public static  string MyDESCryptoDe(string str, byte[] keys, byte[] ivs)
        {
            //解密
            byte[] strs = Convert.FromBase64String(str);

            DESCryptoServiceProvider desc = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();

            ICryptoTransform transform = desc.CreateDecryptor(keys, iv);//解密对象

            CryptoStream cStream = new CryptoStream(mStream, transform, CryptoStreamMode.Write);
            cStream.Write(strs, 0, strs.Length);
            cStream.FlushFinalBlock();
            return Encoding.Unicode.GetString(mStream.ToArray());
        }



    }


}
